package cn.akwangl.socket.nio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Scanner;

@Slf4j
public class UserInputHandler implements Runnable {

    private NIOChatClient chatClient;

    public UserInputHandler(NIOChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @Override
    public void run() {
        Scanner sc = new Scanner(System.in);
        log.info("如需停止程序请输入:quit 关键字并回车");

        try {
            while (true) {
                String userMsg = sc.nextLine();
                // 客户端发送消息给服务端
                chatClient.sendMessage(userMsg);

                if (userMsg.equals(NIOChatService.QUIT)) {
                    break;
                }
            }
        } catch (IOException e) {
            log.error("采集用户数据失败", e);
        } finally {
            sc.close();
        }
    }
}